package com.f5.management.config;

import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.MemoryConstrainedCacheManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.*;

/**
 * @ClassName ShiroConfigBean
 * @Description Shiro拦截设置
 * @Author 吴
 * @Date 2021/4/2 10:04
 * @Version 1.0
 **/

@Configuration
public class ShiroConfigBean {

    //注册Realm
    @Bean
    public RealmConfigBean getRealmBean(){
        return new RealmConfigBean();
    }






    //注册安全管理器。无法使用SecurityManager作为安全管理器
    @Bean
    public DefaultWebSecurityManager getSecurityManager(RealmConfigBean realm){

        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();

        securityManager.setRealm(realm);

        return securityManager;
    }

    //注册shiro拦截器
    @Bean("shiroFilterFactoryBean")//需要指定拦截器ID
    public ShiroFilterFactoryBean getShiroBean(DefaultWebSecurityManager securityManager){

        //创建shiro拦截器对象
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //将安全管理器注册到shiro拦截器
        shiroFilterFactoryBean.setSecurityManager(securityManager);



//        shiroFilterFactoryBean.setLoginUrl("/login");

//        Map<String,String> map = new LinkedHashMap<>();
//        map.put("/**","authc");

//        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        //返回拦截器对象
        return shiroFilterFactoryBean;
    }


}
